github action部署到vps

发表于:2022-03-17 10:45技术热度:95喜欢:1

在 ECS 上创建用户,并设置密码

  1. adduser github-action
  2. asswd passwd github-action

免密登陆

  1. 生成密钥
ssh-keygen -t rsa -C 'github-action' -f ~/.ssh/github_action
  1. 复制密钥到远程服务器
# ssh-copy-id
ssh-copy-id -i ~/.ssh/github_action.pub  github-action@nnnnzs.cn
  1. 设置默认登录
    因为同一台电脑可能既有 root 也有其他用户登录 ecs 所以可以配置指定 Host,指定 User,使用不同的密钥
Host nnnnzs.cn
HostName github-action
User github-action
Port 22
PreferredAuthentications publickey
IdentityFile /Users/nnnnzs/.ssh/github_action
UseKeychain yes
AddKeysToAgent yes

赋予部署文件夹权限

sudo chmod -R 777 /dist

设置github-action的 secrets

https://github.com/NNNNzs/upload/settings/secrets/actions

编写自动部署脚本

mkdir -p .github/workflows
touch .github/workflows/build.yml

name: vue3 
on:
  push:
    branches: [master]
  pull_request:
    branches: [master]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: checkout code
        uses: actions/checkout@v2
      - name: Use Node.js
        uses: actions/setup-node@v2
        with:
          node-version: '14'
      - name: install 
        run: npm install
      - name: build
        run: npm run build
      - name: deploy
        uses: appleboy/scp-action@master
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          key: ${{secrets.KEY}}
          port: ${{ secrets.PORT }}
          source: "./dist/"
          target: ${{secrets.PATH}}
curl -H "Content-Type:application/json" -X POST -d '{"title":"自动部署","content":"upload部署完成","url":"https://api.nnnnzs.cn/upload"}' https://api.nnnnzs.cn/Api/msg

curl -H "Content-Type:application/json" -X POST -d '{"uid":"123"}' http://127.0.0.1:3000/rest/test

常用actions

远程调试 tmate

https://github.com/marketplace/actions/debugging-with-tmate